<?php

namespace app\api\controller;

use app\facade\Redis;
use app\facade\Mns;
use think\Validate;
use think\Facade\Config as Configs;
use app\models\{Banner, Customer, Help, Message, Config, CoinsPrice, Notice};
use think\Db;
use oss\Oss;
use think\Facade\Env;


class Other extends Base
{

    public function Other()
    {
        $result['version'] = (new Config())->where('key', 'version')->value('value');
        $result['ios'] = (new Config())->where('key', 'ios')->value('value');
        $result['android'] = (new Config())->where('key', 'android')->value('value');
        $this->result($result, 0, '获取成功');
    }
    //发送短信
    public function sendMns()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'mobile' => 'require',
        ], [
            'mobile.require' => '手机号码必须填写',
        ]);
        if (!$validate->check($data)) {
            $this->result([], 1, $validate->getError());
        }
        $CheckCode = (new Mns)->send($data['mobile']);
        if (!$CheckCode) {
            $this->result([], 1, $CheckCode['msg']);
        }
        $this->result([], 0, $CheckCode['msg']);
    }

    public function CoinsPrice()
    {
        if ($this->request->isPost()) {
            //时间段
            $TimeArr = [];
            for ($i = 0; $i < 7; $i++) {
                $TimeArr[] = date('Y-m-d', time() - $i * 86400);
            }
            $list = (new CoinsPrice())->field('price,date')->order('date desc')->where(['date' => $TimeArr])->select();
            foreach ($list as $k => $lists) {
                if (!empty($list[$k + 1]['price'])) {
                    $lists['rate'] = round(($lists['price'] - $list[$k + 1]['price']) / $lists['price'] * 100, 2);
                } else {
                    $lists['rate'] = 0;
                }
            }
            $this->result($list, 0, '获取成功');
        } else {
            $list = (new CoinsPrice())->field('price,date')->where(['date' => date('Y-m-d')])->find();
            if (empty($list)) {
                $list['price'] = 0;
                $list['date'] = date('Y-m-d');
            }
            $this->result($list, 0, '获取成功');
        }
    }

    public function Banner()
    {
        $result = (new Banner())->field('id,created', true)->select();
        $this->result($result, 0, '获取成功');
    }


    public function Customer()
    {
        $result = (new Customer())->field('id,created', true)->select();
        $this->result($result, 0, '获取成功');
    }


    public function Help()
    {
        $result = (new Help())->field('id,created', true)->select();
        $this->result($result, 0, '获取成功');
    }

    public function Notice()
    {
        $result = (new Notice())->field('id,created', true)->select();
        $this->result($result, 0, '获取成功');
    }

    public function Message()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'content' => 'require|max:200',
            ];
            $msg = [
                'content.require' => '反馈内容必须',
                'content.max' => '反馈内容不宜过多'
            ];
            $validate = Validate::make($rule, $msg);
            if (!$validate->check($data)) {
                $this->result([], 1, $validate->getError());
            }
            $model = new Message();
            $model->data([
                'content' => $data['content'],
                'user_id' => $this->request->user_id,
                'cover' => $data['cover'] ?? '',
            ]);
            $res = $model->save();
            if ($res) {
                $this->result([], 1, '提交失败');
            }
            $this->result([], 1, '提交失败');
        } else {
            $lists = (new Message())->field('id,user_id,status,admin_name', true)->order('id DESC')->where('user_id', $this->request->user_id)->select();
            foreach ($lists as $list) {
                $list['cover'] = json_decode($list['cover']);
            }
            $this->result($lists, 0, '获取成功');
        }
    }

    public function oss()
    {
        if ($this->request->isPost()) {
            $file = $_FILES['file'];
            $oss = new Oss();
            $oss->setUploadType('ram');
            $url = $oss->uploadPostFile($file, 'jtevideo/' . date('Y-m-d'));
            $this->result($url, 0, language('base.success'));
        } else {
            $oss = new Oss();
            $data = $oss->getCredentials();
            $data['regionId'] = Env::get('aliyun.regionId');
            $data['bucket'] = Env::get('aliyun.bucket');
            $data['endpoint'] = Env::get('aliyun.endpoint');
            $this->result($data, 0, language('base.success'));
        }
    }
}
